package com.xudow.app.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.activeshare.edu.ucenter.models.qanda.Question;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.P2PQuestionActivity;
import com.xudow.app.ui.QandaActivity;
import com.xudow.app.ui.QuestionDetailActivity;
import com.xudow.app.ui.adapter.user.QuestionsAdapter;
import com.xudow.app.ui.support.RecyclerViewScrollLocationListener;
import com.xudow.app.ui.support.XLinearLayoutManager;
import com.xudow.app.ui.task.user.QuestionsTask;
import com.xudow.app.util.Lists;
import com.xudow.app.util.Maps;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 我的提问Activity
 *
 * @author lukecheng
 */
public class QuestionsActivity extends BaseActivity
        implements QuestionsAdapter.OnRecyclerViewListener {

    private List<Question> questions;
    private QuestionsAdapter questionsAdapter;

    private RelativeLayout emptyView;
    private RecyclerView recyclerView;
    private ProgressBar progressBar;

    private int page = 1;

    private QuestionsTask questionsTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);

        emptyView = (RelativeLayout) findViewById(R.id.empty_view);
        recyclerView = (RecyclerView) findViewById(R.id.question_list);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        recyclerView.setHasFixedSize(true);
        XLinearLayoutManager layoutManager = new XLinearLayoutManager(this);
        layoutManager.setOnRecyclerViewScrollLocationListener(recyclerView,
                new RecyclerViewScrollLocationListener() {
                    @Override
                    public void onTopWhenScrollIdle(RecyclerView recyclerView) {
                    }

                    @Override
                    public void onBottomWhenScrollIdle(RecyclerView recyclerView) {
                        //设置页数
                        page++;
                        loadQuestions();
                    }
                });
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        questions = Lists.newArrayList();
        questionsAdapter = new QuestionsAdapter(this, questions);
        questionsAdapter.setOnRecyclerViewListener(this);
        recyclerView.setAdapter(questionsAdapter);

        loadQuestions();
    }

    private void loadQuestions() {
        //分页
        HashMap<String, String> params = Maps.newHashMap();
        params.put(Config.PAGINATION_PARAM_NAME, "" + this.page);

        questionsTask = new QuestionsTask(this, questionsTaskHandler);
        questionsTask.execute(params);
    }

    /**
     * 返回按钮点击事件
     *
     * @param view
     */
    public void onBackBtnClick(View view) {
        finish();
    }

    private Handler questionsTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (QuestionsTask.SUCCESS == msg.what) {
                Question[] dataArr = (Question[]) msg.getData().getSerializable("questions");

                if (null == dataArr || dataArr.length <= 0) {
                    if (page <= 1) {
                        initEmptyView();
                    }

                    if (page > 1) {
                        page--;
                    }

                    return;
                }

                progressBar.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);

                questions.addAll(Arrays.asList(dataArr));
                questionsAdapter.notifyDataSetChanged();
            }
        }
    };

    private void initEmptyView() {
        progressBar.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    }

    @Override
    public void onItemClick(int position) {
        Question question = questions.get(position);

        //1: 1对1问专家 2：不针对专家问题，任何人都可以回答
        int type = question.getType();

        Intent intent = null;

        if (type == 1) {
            intent = new Intent(this, P2PQuestionActivity.class);
        } else {
            intent = new Intent(this, QuestionDetailActivity.class);
        }

        intent.putExtra("questionId", question.getId());
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }

    @Override
    protected void onDestroy() {
        if (null != questionsTask && !questionsTask.isCancelled()) {
            questionsTask.cancel(true);
            questionsTask = null;
        }

        super.onDestroy();
    }

    /**
     * 提问点击事件
     *
     * @param view
     */
    public void onAskClick(View view) {
        startActivity(new Intent(this, QandaActivity.class));
        finish();
    }
}
